home *** CD-ROM | disk | FTP | other *** search
- /*
- File: NativeThreadTestApp.c
-
- Contains: Test code for the NativeThreadTestApp.
-
- Written by: Brad Post
-
- Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <2> 9/14/93 bsp Added comments and stuff
- <1> 8/13/93 bsp first checked in
- */
-
- #include "NativeThreadTestApp.h"
-
- /*
- * Globals needed for the test application.
- */
-
- QDGlobals qd;
- MenuHandle myMenus[3];
- WindowPtr myWindow;
- Rect myWindRect;
- short x, y;
- short threadCounter;
- ThreadID theThreadIDs[20];
-
-
- #define LoWord(x) ((short)(x))
- #define HiWord(x) ((short)((long)(x) >> 16))
-
-
- /*
- * aThread
- *
- * This thread just finds out what it's ThreadID is and then prints it to the screen.
- */
- pascal void *aThread( void *threadParameter )
- {
- Str255 myThreadID;
- ThreadID theTID, aThreadID;
-
- aThreadID = *((ThreadID *) threadParameter);
-
- if( GetCurrentThread( &theTID ) != noErr )
- myAlert("\pGetCurrentThread failed");
-
- NumToString( theTID, &myThreadID );
-
- if( theTID != aThreadID )
- myAlert("\pThread ID's didn't match");
-
- while(1)
- {
- myNotify( myThreadID );
- YieldToAnyThread();
- }
- }
-
-
- /*
- * doMenus
- *
- * This function just handles menus.
- */
- doMenus(long selector)
- {
- short whichMenu = HiWord(selector);
- short whichItem = LoWord(selector);
- GrafPtr savePort;
- Str255 name;
-
- switch(whichMenu)
- {
- case appleMenuID:
- switch(whichItem)
- {
- case 1: Alert(aboutALRT, 0L);
- break;
- default:
- {
- GetPort(&savePort);
- GetItem(myMenus[0], whichItem, name);
- OpenDeskAcc(name);
- SetPort(savePort);
- }
- }
- break;
- case fileMenuID:
- switch(whichItem)
- {
- case fmNew:
- createNewThread();
- break;
- case fmQuit:
- ExitToShell();
- break;
- }
- break;
- }
- }
-
- // Just the main routine
-
- main()
- {
- char c;
- short done = 0;
- short whichWindow;
- WindowPtr theWindow;
- EventRecord myEvent;
- Str255 myThreadID;
- ThreadID theTID;
-
- InitAll();
-
- if( GetCurrentThread( &theTID ) != noErr )
- myAlert("\pGetCurrentThread failed");
-
- NumToString( theTID, &myThreadID );
-
- while(!done)
- {
- if(GetNextEvent(-1, &myEvent) == true)
- {
- switch(myEvent.what)
- {
- case mouseDown:
- {
- whichWindow = FindWindow(myEvent.where, &theWindow);
- switch(whichWindow)
- {
- case inSysWindow:
- SystemClick(&myEvent, theWindow);
- break;
- case inMenuBar:
- doMenus(MenuSelect(myEvent.where));
- break;
- }
- }
- break;
- case keyDown:
- case autoKey:
- if((myEvent.modifiers & cmdKey) != 0)
- doMenus(MenuKey( (char) (myEvent.message & charCodeMask)));
- break;
- }
- }
- myNotify( myThreadID );
- YieldToAnyThread();
- }
- }
-
- /*
- * myAlert
- *
- * Just throws up a ALRT box with a specified string.
- */
- myAlert(Str255 theString)
- {
- ParamText(theString, NULL, NULL, NULL);
- Alert(errorALRT, NULL);
- }
-
-
- InitAll()
- {
- InitGraf( &(qd.thePort) );
- InitFonts();
- FlushEvents( everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- MaxApplZone();
-
- myMenus[1] = GetMenu(fileMenuID);
- InsertMenu(myMenus[0] = NewMenu(1, "\p\024"), 0);
- InsertMenu(myMenus[1], 0);
- AppendMenu(myMenus[0], "\pAbout NativeThreadTestApp;(-");
- AddResMenu(myMenus[0], 'DRVR');
- DrawMenuBar();
-
- myWindRect.top = 40;
- myWindRect.bottom = qd.screenBits.bounds.bottom - 20; // windRect.top + 430
- myWindRect.left = 10;
- myWindRect.right = qd.screenBits.bounds.right - 20; // windRect.left + 600
-
- myWindow = NewWindow(0L, &myWindRect, "\pTestAppWindow", true, documentProc,
- (WindowPtr) -1, false, 0);
-
- x = myWindRect.left + 10;
- y = myWindRect.top + 5;
-
- threadCounter = 0;
-
- createNewThread();
- }
-
- /*
- * createNewThread
- *
- * This function just creates a new cooperative thread.
- */
- createNewThread()
- {
- #ifdef DEBUG
- DebugStr("\pcreating a NewThread from App");
- #endif
-
- if( NewThread( kCooperativeThread, aThread, (void *) theThreadIDs[threadCounter], 0L, kCreateIfNeeded, 0L, theThreadIDs[threadCounter]) != noErr )
- myAlert("\pGot an error creating a thread");
- }
-
- /*
- * myNotify
- *
- * Just prints out the message to the the window.
- */
- myNotify( Str255 theMessage )
- {
- SetPort( myWindow );
-
- PenNormal();
- TextFont(geneva);
- TextSize(12);
-
- if( (y + 20) > myWindRect.bottom )
- {
- EraseRect(&(**myWindow->visRgn).rgnBBox);
- x = myWindRect.left + 10;
- y = myWindRect.top + 10;
- }
-
- MoveTo(x, y);
-
- DrawString( theMessage );
-
- y += 10;
-
- }
-